package com.iye.puretennis.fragment;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import com.iye.puretennis.helper.Constant;
import com.iye.puretennis.helper.DBManager;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * Author: Chuanbao on 2014/11/25.
 */
public class TabChildFragment extends Fragment {

    protected View mMainView;

    protected ImageButton backButton;

    protected Typeface listFace;

    protected DBManager dbManager;


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        listFace = Typeface.createFromAsset(getActivity().getAssets(), Constant.DEFAULT_FONT);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    protected void onBackPressed() {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        int count = fm.getBackStackEntryCount();
        if(count > 0){
            fm.popBackStack();
        }
        transaction.commit();
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    protected void goFragment(int sourceId, String tag, Class targetFragmentClazz, Bundle args, boolean alwaysNewTarget) {
        FragmentManager fm = getChildFragmentManager();
        // 开启Fragment事务
        FragmentTransaction transaction = fm.beginTransaction();

        Fragment fragment = null;
        if(!alwaysNewTarget){
            fragment = fm.findFragmentByTag(tag);
        }

        if (fragment == null) {
            Constructor constructor;
            try {
                constructor = targetFragmentClazz.getConstructor(new Class[0]);
                fragment = (Fragment) constructor.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
        }
        if (args != null) {
            fragment.setArguments(args);
        }
        transaction.replace(sourceId, fragment, tag).
                setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right)
                .addToBackStack(null).commit();
    }

    //enable为true时，菜单添加图标有效，enable为false时无效。4.0系统默认无效
    protected void setIconEnable(Menu menu, boolean enable) {
        try {
            Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder");

            Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);

            m.setAccessible(true);

            //MenuBuilder实现Menu接口，创建菜单时，传进来的menu其实就是MenuBuilder对象(java的多态特征)
            m.invoke(menu, enable);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
